<!-- pages/practice/practice.vue -->
<template>
  <view class="container">
    <!-- 搜索框 -->
    <view class="header">
      <view class="search-box">
        <text class="search-icon">🔍</text>
        <input type="text" placeholder="搜索你感兴趣的题目" class="search-input"/>
      </view>
    </view>
    
    <!-- 练习分类 -->
    <view class="exercise-types">
      <view class="type-item" @click="navigateTo('/pages/exercise/undone')">
        <image src="/static/练习题- 彩色.png" mode="aspectFit" class="type-icon"></image>
        <text class="type-text">未做练习 ✨</text>
        <text class="type-desc">开始新的挑战吧！</text>
      </view>
      <view class="type-item" @click="navigateTo('/pages/exercise/done')">
        <image src="/static/练习题- 彩色.png" mode="aspectFit" class="type-icon"></image>
        <text class="type-text">已做练习 📝</text>
        <text class="type-desc">查看历史记录~</text>
      </view>
    </view>
    
    <!-- 最近练习 -->
    <view class="recent-exercises">
      <view class="section-title">
        <text class="title-icon">📚</text>
        <text>最近练习</text>
        <text class="refresh-text">点击刷新 🔄</text>
      </view>
      <view class="exercise-list">
        <view class="exercise-item" v-for="(item, index) in recentExercises" :key="index">
          <view class="exercise-info">
            <text class="exercise-name">{{item.name}}</text>
            <text class="exercise-time">{{item.time}} 🕒</text>
          </view>
          <view class="exercise-score" :class="{'high-score': item.score >= 80}">
            {{item.score}}分 {{item.score >= 80 ? '🎉' : '💪'}}
          </view>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="bottom-tab-bar">
      <view class="tab-item" 
            v-for="(item, index) in bottomNavItems" 
            :key="index"
            @tap="switchTab(index)">
        <image :src="currentTab === index ? item.activeIcon : item.icon" 
               mode="aspectFit" 
               class="tab-icon"></image>
        <text class="tab-text"
              :class="{ 'active': currentTab === index }">{{ item.text }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

// 最近练习数据
const recentExercises = ref([
  { name: '2023年数学一真题', time: '2024-12-29', score: 89 },
  { name: '2022年数学一真题', time: '2024-12-28', score: 76 },
  { name: '函数极限练习题', time: '2024-12-27', score: 92 }
])

// 当前选中的tab（练习页是第二个tab，索引为1）
const currentTab = ref(1)

// 底部导航数据
const bottomNavItems = ref([
  { 
    icon: '/static/icons/politics.png',
    activeIcon: '/static/icons/politics.png', 
    text: '首页',
    url: '/pages/index/index'
  },
  { 
    icon: '/static/icons/练习.png',
    activeIcon: '/static/icons/练习.png', 
    text: '练习',
    url: '/pages/practice/practice'
  },
  { 
    icon: '/static/icons/home1.png',
    activeIcon: '/static/icons/home1.png', 
    text: '社区',
    url: '/pages/community/index'
  },
  { 
    icon: '/static/icons/user.png',
    activeIcon: '/static/icons/user-active.png', 
    text: '我的',
    url: '/pages/my/my'
  }
])

// 页面导航方法
const navigateTo = (url) => {
  uni.navigateTo({
    url
  })
}

// 底部标签页切换方法
const switchTab = (index) => {
  const item = bottomNavItems.value[index]
  currentTab.value = index
  
  uni.switchTab({
    url: item.url,
    success: () => {
      console.log('页面跳转成功')
    },
    fail: (err) => {
      console.error('切换标签页失败：', err)
      uni.navigateTo({
        url: item.url,
        fail: (navigateErr) => {
          console.error('导航失败：', navigateErr)
          uni.showToast({
            title: '页面跳转失败',
            icon: 'none'
          })
        }
      })
    }
  })
}
</script>

<style>
.container {
  padding: 20rpx;
  padding-bottom: 120rpx;
  background-color: #f8f9fa;
}

.header {
  margin-bottom: 30rpx;
}

.search-box {
  background-color: #ffffff;
  padding: 20rpx;
  border-radius: 30rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.search-icon {
  margin-right: 20rpx;
  font-size: 32rpx;
}

.search-input {
  flex: 1;
  height: 60rpx;
  font-size: 28rpx;
}

.exercise-types {
  display: flex;
  justify-content: space-between;
  margin-bottom: 40rpx;
}

.type-item {
  width: 48%;
  background-color: #ffffff;
  padding: 30rpx;
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.type-item:active {
  transform: scale(0.98);
}

.type-icon {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 20rpx;
}

.type-text {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.type-desc {
  font-size: 24rpx;
  color: #999;
}

.section-title {
  display: flex;
  align-items: center;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
}

.title-icon {
  margin-right: 16rpx;
  font-size: 40rpx;
}

.refresh-text {
  font-size: 24rpx;
  color: #999;
  margin-left: auto;
  font-weight: normal;
}

.exercise-list {
  background-color: #ffffff;
  border-radius: 20rpx;
  padding: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.exercise-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  border-bottom: 1rpx solid #f5f5f5;
  transition: all 0.3s ease;
}

.exercise-item:active {
  background-color: #f8f9fa;
}

.exercise-item:last-child {
  border-bottom: none;
}

.exercise-info {
  display: flex;
  flex-direction: column;
}

.exercise-name {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
  font-weight: 500;
}

.exercise-time {
  font-size: 24rpx;
  color: #999;
}

.exercise-score {
  font-size: 32rpx;
  font-weight: bold;
  color: #ff6b6b;
}

.exercise-score.high-score {
  color: #A8BF8F;
}

/* 底部导航栏样式 */
.bottom-tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1rpx solid #eeeeee;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 999;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10rpx 0;
  position: relative;
}

.tab-item:active {
  opacity: 0.8;
}

.tab-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 6rpx;
}

.tab-text {
  font-size: 24rpx;
  color: #666666;
  line-height: 1.2;
}

.tab-text.active {
  color: #A8BF8F;
  font-weight: bold;
}

/* 适配iPhone底部安全区 */
@supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
  .bottom-tab-bar {
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
  .container {
    background-color: #000000;
  }

  .search-box,
  .type-item,
  .exercise-list {
    background-color: #1c1c1e;
  }

  .search-input,
  .type-text,
  .exercise-name {
    color: #ffffff;
  }

  .type-desc,
  .exercise-time,
  .refresh-text {
    color: #8e8e93;
  }

  .bottom-tab-bar {
    background-color: #1c1c1e;
    border-top-color: #2c2c2e;
  }
  
  .tab-text {
    color: #8e8e93;
  }
  
  .tab-text.active {
    color: #A8BF8F;
  }

  .exercise-item {
    border-bottom-color: #2c2c2e;
  }

  .exercise-item:active {
    background-color: #2c2c2e;
  }
}
</style>